package com.base.string;

import java.util.HashMap;
import java.util.Map;

/**
 * 299. 猜数字游戏
 *https://leetcode-cn.com/problems/bulls-and-cows/
 * @author leon
 * @date 2021年11月08日 19:02
 */
public class GetHint {
    public String getHint(String secret, String guess) {
        int m = secret.length();
        Map<Character,Integer> secretMap = new HashMap<>();
        Map<Character,Integer> guessMap = new HashMap<>();
        int A = 0;
        for(int i = 0;i<m;i++){
            if(secret.charAt(i) == guess.charAt(i)){
                A++;
                continue;
            }
            Integer integer = secretMap.containsKey(secret.charAt(i)) ? secretMap.put(secret.charAt(i), secretMap.get(secret.charAt(i)) + 1) : secretMap.put(secret.charAt(i), 1);
            integer = guessMap.containsKey(guess.charAt(i)) ? guessMap.put(guess.charAt(i), guessMap.get(guess.charAt(i)) + 1) : guessMap.put(guess.charAt(i), 1);

        }
        int B = 0;
        for (Character a : guessMap.keySet()) {
            if (secretMap.containsKey(a)){
                B+=Math.min(secretMap.get(a),guessMap.get(a));
            }
        }
        return A+"A"+B+"B";
    }
    public String getHint1(String secret,String guess){
        int[] guessMap = new int[10];
        int[] secretMap = new int[10];
        int A = 0;
        for (int i = 0; i < secret.length(); i++) {
            if (secret.charAt(i) == guess.charAt(i)){
                A++;
                continue;
            }
            guessMap[guess.charAt(i) - '0'] += 1;
            secretMap[secret.charAt(i) - '0'] += 1;
        }

        int B = 0;
        for (int i = 0; i < 10; i++) {
            B+=Math.min(secretMap[i],guessMap[i]);
        }

        return A+"A"+B+"B";
    }
}
